import * as PIXI from "pixi.js-legacy";
import { ShaderSystem } from '@pixi/core';
import { install } from '@pixi/unsafe-eval';
// Apply the patch to PIXI、
import { IPointData } from "pixi.js";

import { Live2DModel } from './js/libs/live2d/cubism4';


install({ ShaderSystem });

export default class Bootstrap {


    public start() {
        Live2DModel.registerTicker(PIXI.Ticker);
        PIXI.Renderer.registerPlugin('interaction', PIXI.InteractionManager);
        const { pixelRatio, windowWidth, windowHeight } = wx.getSystemInfoSync();

        canvas.width = windowWidth * pixelRatio;
        canvas.height = windowHeight * pixelRatio;
        let game = new PIXI.Application({
            width: windowWidth * pixelRatio,
            height: windowHeight * pixelRatio,
            antialias: true,
            view: canvas,
        });

        game.renderer.plugins.interaction.mapPositionToPoint = (point: IPointData, x: number, y: number) => {
            point.x = x * pixelRatio
            point.y = y * pixelRatio
        }

        let width = windowWidth * pixelRatio;
        let height = windowHeight * pixelRatio;


        let background = new PIXI.Sprite();

        let bgGraph = new PIXI.Graphics();
        bgGraph.beginFill(0xEBE0CE);
        bgGraph.drawRect(0, 0, width, height)
        bgGraph.endFill()
        background.addChild(bgGraph)
        let bgImg = new PIXI.Sprite(PIXI.Texture.from(`images/bg.png`))
        bgImg.width = width;
        bgImg.height = height;
        background.addChild(bgImg)
        let container = new PIXI.Container()
        container.addChild(background)
        game.stage.addChild(container);

        let modelContainer = new PIXI.Container()
        let model = Live2DModel.fromSync("https://novid-defend.oss-cn-hangzhou.aliyuncs.com/hunters/default/hunter_l2d/hunter.model3.json", { onError: console.warn });
        model.once('load', () => {
            model.autoInteract = true;
            model.anchor.set(0.5, 0.5);
            model.interactive = true;
            model.x = width / 2;
            model.y = height / 2;
            modelContainer.addChild(model);
        });
        game.stage.addChild(modelContainer);

    };



    public preLoad(): void {

    }
}